import { _decorator, Component, Node, Prefab, SpriteFrame, assetManager, instantiate, Sprite } from 'cc';
import { ResLoader } from './ResLoader';

const { ccclass, property } = _decorator;

@ccclass('ExampleUsage')
export class ExampleUsage extends Component {
    private prefabKey = 'prefabs/character';
    private imageKey = 'images/icon';
    private sprite: Sprite;

    async onLoad() {
        // 加载预制体
        const prefab = await ResLoader.loadPrefab(this.prefabKey, 'game');
        const node = instantiate(prefab);
        this.node.addChild(node);

        // 加载图片
        const spriteFrame = await ResLoader.loadSpriteFrame(this.imageKey, 'ui');
        this.sprite = this.node.getComponent(Sprite)!;
        this.sprite.spriteFrame = spriteFrame;

        console.log(assetManager.assets)
        console.log("spriteFrame.refCount : " + this.sprite.spriteFrame.refCount);

    }

    onDestroy() {
        // 释放资源
        ResLoader.release(this.prefabKey, 'game');
        ResLoader.release(this.imageKey, 'ui');
    }
}
